def p(x):
    f = True
    for i in range(2,x):
        if x % i == 0:
            f = False
            break
    if f == False:
        return True
    else:
        return False


n = int(input())
if n < 999 or n > 10000:
    print(0)
else:
    n1 = n 
    n2 = n // 10
    n3 = n % 1000
    n4 = n2 // 10
    n5 = n3 // 10
    n6 = n3 % 100
    n7 = n // 1000 
    n8 = n6 // 10
    n9 = n6 % 10
    n10 = n% 10
    m = 0
    if p(n) == False and n > m:
       m = n
    if p(n1) == False and n1 > m:
       m = n1
    if p(n2) == False and n2 > m:
       m = n2
    if p(n3) == False and n3 > m:
       m = n3
    if p(n4) == False and n4 > m:
       m = n4
    if p(n5) == False and n5 > m:
       m = n5
    if p(n6) == False and n6 > m:
       m = n6
    if p(n7) == False and n7 > m:
       m = n7
    if p(n8) == False and n8 > m:
       m = n8
    if p(n9) == False and n9 > m:
       m = n9
    if p(n10) == False and n10 > m:
       m = n10
print(m)